package com.leyou.gateway.config;

import com.leyou.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.security.PublicKey;

/**
 * description: 自定义JWT配置属性类
 * @author 朱正磊
 * @date 2021-04-18 00:34:56
 */
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties {

    /**
     * 公钥地址
     */
    private String pubKeyPath;

    /**
     * cookie名称
     */
    private String cookieName;

    /**
     * 公钥
     */
    private PublicKey publicKey;

    /**
     * 本类一旦被实例化后即类加载完成之后，就应该读取公钥
     */
    @PostConstruct
    public void init() {
        try {
            // 读取公钥
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            log.error("【网关微服务】读取公钥异常！", e);
            throw new RuntimeException();
        }
    }

}
